package com.jiong.controller;

import com.jiong.common.util.PageResult;
import com.jiong.common.util.R;
import com.jiong.entity.Songs;
import com.jiong.service.ISongsService;
import com.jiong.vo.SongVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiong
 * @since 2025-01-13
 */
@RestController
@RequestMapping("/songs")
public class SongsController {
    @Autowired
    ISongsService songsService;

    @RequestMapping("/list")
    public R getStudentList() {
        return R.ok().data("list", songsService.list());
    }

    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page,
                        @PathVariable("pageSize") Integer pageSize) {
        PageResult<Songs> pageResult = songsService.findByPage(page,pageSize);
        return R.ok().data("pageResult",pageResult);
    }

    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page,
                   @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) SongVO songVO) {
        PageResult<Songs> pageResult = songsService.search(page, pageSize, songVO);
        return R.ok().data("pageResult", pageResult);
    }

    @PostMapping("/add")
    public R addStudent(@RequestBody Songs songs) {
        boolean isAdded = songsService.add(songs);
        if (isAdded) {
            return R.ok().message("歌单添加音乐成功！");
        } else {
            return R.error().message("歌单添加音乐失败！");
        }
    }

    @PutMapping("/update")
    public R updateStudent(@RequestBody Songs songs) {
        boolean isUpdated = songsService.updateById(songs);
        if ( isUpdated ) {
            return R.ok().message("修改音乐信息成功！");
        } else {
            return R.error().message("修改音乐信息失败！");
        }
    }

    @DeleteMapping("/delete/{songsIds}")
    public R deleteStudents(@PathVariable("songsIds") String songIds) {
        String[] idArray = songIds.split(",");
        List<String> songIdList = Arrays.asList(idArray);
        boolean isDeleted = songsService.removeByIds(songIdList);
        if (isDeleted) {
            return R.ok().message("删除音乐成功！");
        } else {
            return R.error().message("删除音乐失败！");
        }
    }

}
